package com.starwish.starwishblog.web.back;


import com.github.pagehelper.PageInfo;
import com.starwish.starwishblog.entity.Body;
import com.starwish.starwishblog.result.ResultResponse;
import com.starwish.starwishblog.service.AdviceService;
import com.starwish.starwishblog.vo.AdviceVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("admin/advice")
@Api(value = "对公告的操作")
public class AdviceController {
    @Autowired
    AdviceService adviceService;

    @PostMapping("/addAdvice")
    @ApiOperation(value = "添加公告!")
    public ResultResponse<Void> addAdvice(@RequestBody AdviceVo adviceVo){
        ResultResponse<Void> result = null;
        try {
            adviceVo.setPublishTime(new Date());
            int i = adviceService.addAdvices(adviceVo);
            result = new ResultResponse<>(200,"添加公告成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201,"添加公告失败!");
        }
        return result;
    }

    @DeleteMapping("/deleteAdvice/{id}")
    @ApiOperation(value = "删除公告!")
    public ResultResponse<Void> deleteAdvice(@PathVariable("id") int id){
        ResultResponse<Void> result = null;
        try {
            int i = adviceService.removeAdvice(id);
            result = new ResultResponse<>(200,"删除公告成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201,"删除公告失败!");
        }
        return result;
    }


    @GetMapping("/cancelAdvice")
    @ApiOperation(value = "撤销/恢复公告!")
    public ResultResponse<Void> cancelAdvice(@RequestParam("id") Integer id,@RequestParam("status") Integer status){
        ResultResponse<Void> result = null;
        try {
            int i = adviceService.cancelAdviceById(id,status);
            result = new ResultResponse<>(200,"撤销公告成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201,"撤销公告失败!");
        }
        return result;
    }

    @GetMapping("/topAdvice")
    @ApiOperation(value = "置顶/取消置顶公告!")
    public ResultResponse<Void> topAdvice(@RequestParam("id") Integer id,@RequestParam("status") Integer status){
        ResultResponse<Void> result = null;
        try {
            int i = adviceService.topAdviceById(id,status);
            result = new ResultResponse<>(200,"操作成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201,"操作失败!");
        }
        return result;
    }

    @GetMapping("/bodyList")
    @ApiOperation(value = "内容查询!")
    public ResultResponse<List<Body>> bodyList(){
        ResultResponse<List<Body>> result = null;
        try {
            List<Body> bodyList = adviceService.queryBodyAll();
            result = new ResultResponse<>(200,"内容查询成功!",bodyList);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(200,"内容查询失败!");
        }
        return result;
    }

    @PutMapping("/modifyAdvice")
    @ApiOperation(value = "修改公告!")
    public ResultResponse<Void> modifyAdvice(@RequestBody AdviceVo adviceVo){
        ResultResponse<Void> result = null;
        try {
            int i = adviceService.modify(adviceVo);
            result = new ResultResponse<>(200,"修改公告成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201,"修改公告失败!");
        }
        return result;
    }

    @GetMapping("/showAdviceByPage")
    @ApiOperation(value = "分页查询公告!")
    public ResultResponse<PageInfo<AdviceVo>> showAdviceByPage(@RequestParam(value = "now",defaultValue = "1") Integer now,
            @RequestParam(value = "size",defaultValue = "5")Integer size, String title, Map map){
        ResultResponse<PageInfo<AdviceVo>> result = null;
        try {
            PageInfo<AdviceVo> pageInfo = adviceService.queryAdviceByPage(now, size, title);
            result = new ResultResponse<>(200,"查询成功",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询失败");
        }
        return result;
    }

    @GetMapping("/showAdviceByWeight")
    @ApiOperation(value = "按置顶权重降序排序查询公告!")
    public ResultResponse<PageInfo<AdviceVo>> showAdviceByWeight(@RequestParam(value = "now",defaultValue = "1") Integer now,
            @RequestParam(value = "size",defaultValue = "5")Integer size, String title, Map map){
        ResultResponse<PageInfo<AdviceVo>> result = null;
        try {
            PageInfo<AdviceVo> pageInfo = adviceService.queryAdviceByWeight(now, size, title);
            result = new ResultResponse<>(200,"排序成功",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201,"排序失败");
        }
        return result;
    }



}
